{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "63378f93",
   "metadata": {},
   "source": [
    "## 1.生成1的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ac824cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.np.ones\n",
    "import numpy as np\n",
    "# 创建一个3x2的数组，元素值全为1，数据类型为默认的float\n",
    "array1 = np.ones((3,2))\n",
    "print(\"默认类型：\\n\", array1)\n",
    "\n",
    "# 创建一个2x4的数组，元素值全为1，数据类型为int\n",
    "array2 = np.ones((2,4),dtype=int)\n",
    "print(\"\\n指定类型：\\n\", array2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "870e223f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.np.ones_like\n",
    "# 给定一个参考数组\n",
    "ref_array = np.array([[10, 20, 30], [40, 50, 60]])\n",
    "# 创建与ref_array形状相同的数组，元素值全为1\n",
    "array3 = np.ones_like(ref_array)\n",
    "print(\"\\n与参考数组形状相同，元素值全为1：\\n\", array3)\n",
    "\n",
    "# 指定数据类型为float\n",
    "array4 = np.ones_like(ref_array, dtype=float)\n",
    "print(\"\\n与参考数组形状相同，元素值全为1，指定数据类型为float：\\n\", array4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9397afd2",
   "metadata": {},
   "source": [
    "## 2.生成0的数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f3d4668",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.np.zeros(shape, dtype)\n",
    "# 创建一个3x2的数组，元素值全为0\n",
    "array5 = np.zeros((3,2))\n",
    "print(\"\\n默认类型：\\n\", array5)\n",
    "\n",
    "# 创建一个2x3的数组，数据类型为int\n",
    "array6 = np.zeros((2,3),dtype=int)\n",
    "print(\"\\n指定类型：\\n\", array6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fb7ef34a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2.np.zeros_like(a, dtype)\n",
    "# 给定一个参考数组\n",
    "ref_array = np.array([[5, 10, 15], [20, 25, 30]])\n",
    "# 创建与ref_array形状相同的数组，元素值全为0\n",
    "array7 = np.zeros_like(ref_array)\n",
    "\n",
    "# 指定数据类型为int\n",
    "array8 = np.zeros_like(ref_array, dtype=int)\n",
    "print(\"\\n与参考数组形状相同，元素值全为0：\\n\", array7)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
